<?php
include_once 'simple_html_dom.php';
include_once  'xml.class.php';
ini_set('memory_limit', '512M');
set_time_limit(0);

//把節氣換成XML
//id  2012-01  立春  type 節 startTime =?

function getSrcFileName($i)
{
    return 'jachi/jachi-'.$i.'.html';
}

function getDestFileName($i)
{
    return 'jachis/'.$i.'.xml';
}

function getSerialFileName($i)
{
    return 'jachis/'.$i.'.ser';
}



function isJachi($s)
{
    $jachis=array('立春','雨水','惊蛰',
            '春分','清明','谷雨',
            '立夏','小满','芒种',
            '夏至','小暑','大暑',
            '立秋','处暑','白露',
            '秋分','寒露','霜降',
            '立冬','小雪','大雪',
            '冬至','小寒','大寒');
    

    $cs=count($jachis);
    for($i=0;$i<$cs;$i++)
    {
        if($jachis[$i]==$s)
            return $i;
    }
    return  -1;
}

function processJaChi($node,$y)
{
    $jachi2=array('立春','雨水','驚蟄',
            '春分','清明','穀雨',
            '立夏','小滿','芒種',
            '夏至','小暑','大暑',
            '立秋','處暑','白露',
            '秋分','寒露','霜降',
            '立冬','小雪','大雪',
            '冬至','小寒','大寒');
     
    
    $result=array();
    $jachiName=trim($node->plaintext);
    $id=isJachi($jachiName);
    $jachiName=$jachi2[$id];
    $result['name']=$jachiName;

    if($id>=22)
    {
        $result['id']=($y+1).'-'.$id;
    }else
    {
        $result['id']=$y.'-'.$id;
    }

    if($id%2==0)
    {
        $result['type']='節';
    }else
    {
        $result['type']='氣';
    }

    $td=$node->next_sibling();
    $result['t']=$td->innertext;
    $t=$td->innertext;
    $t=str_replace('月','-',$t);
    $t=str_replace('日','',$t);

    if($id>=22)
    {
        $t=($y+1).'-'.$t;
    }else
    {
        $t=$y.'-'.$t;
    }
    $tt=strtotime($t);
    $result['time']=date('Y-m-d H:i:s',$tt);
    return $result;
}

function toXML($datas,$y)
{
    $s="";
    foreach($datas as $entry)
    {
        $s.='<jachi>'.xml_serialize_X($entry,0,2).'</jachi>';
    }
    return '<?xml version="1.0" encoding="UTF-8"?><data  year="'.$y.'"  >'.$s.'</data>';
}
function processYear($y)
{
    $fn=getDestFileName($y);
    if(file_exists($fn))
        return;

    $fileName=getSrcFileName($y);

    if(file_exists($fileName)==false)
        return;

    $page= file_get_contents($fileName);
    $html = str_get_html($page);
    $ths=$html->find('th');
    $jachi=array();
    foreach($ths as $th)
    {
        $ss=trim($th->plaintext);
        if(isJachi($ss)>=0)
        {
            $jachi[]=processJaChi($th,$y);
        }
    }
    
    $ser1=serialize($jachi);
    $fn2=getSerialFileName($y);    
    file_put_contents($fn2,$ser1);
    
    
    $ser=toXML($jachi,$y);
    file_put_contents($fn,$ser);
}

for($i=1902;$i<=2120;$i++)
{
    processYear($i);
}










